/**
 * Copyright (C) 2008 aileron.cc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package cc.aileron.commons.image;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.stream.FileImageInputStream;

import com.sun.imageio.plugins.jpeg.JPEGImageReader;
import com.sun.imageio.plugins.jpeg.JPEGImageReaderSpi;
import com.sun.imageio.plugins.png.PNGImageReader;
import com.sun.imageio.plugins.png.PNGImageReaderSpi;

/**
 * 画像のリサイズ
 * @author Aileron
 *
 */
public class ResizeImageUtils
{
    /**
     * 画像の種類
     * @author Aileron
     *
     */
    public static enum Type
    {
        PNG()
        {
            @Override
            public BufferedImage image(FileImageInputStream stream)
                    throws IOException
            {
                final PNGImageReader imageReader = new PNGImageReader(new PNGImageReaderSpi());
                imageReader.setInput(stream);
                final BufferedImage image = imageReader.read(0);
                stream.close();
                return image;
            }
            
        },
        JPEG()
        {
            @Override
            public BufferedImage image(FileImageInputStream stream)
                    throws IOException
            {
                final JPEGImageReader imageReader = new JPEGImageReader(new JPEGImageReaderSpi());
                imageReader.setInput(stream);
                final BufferedImage image = imageReader.read(0);
                stream.close();
                return image;
            }
        };
        
        public abstract BufferedImage image(final FileImageInputStream stream) throws IOException;
    }
    
    /**
     * png画像のBufferedImageを取得する
     * @param stream 入力ストリーム
     * @return 画像のピクセルデータ
     * @throws IOException
     */
    public static BufferedImage pngImage(final FileImageInputStream stream) throws IOException
    {
        return Type.PNG.image(stream);
    }
    
    /**
     * jpeg画像のBufferedImageを取得する
     * @param stream　入力ストリーム
     * @return 画像のピクセルデータ
     * @throws IOException
     */
    public static BufferedImage jpegImage(final FileImageInputStream stream) throws IOException
    {
        return Type.JPEG.image(stream);
    }
    
    /**
     * 画像のリサイズ
     * @param stream 画像のピクセルデータのストリーム
     * @param width 幅
     * @param height 高さ
     * @return リサイズ後の画像のピクセルデータのストリーム
     * @throws IOException 
     */
    public static BufferedImage resizeImage(
            final BufferedImage image,
            final int width,
            final int height) throws IOException
    {
        final BufferedImage resizeImage = new BufferedImage(width,height,BufferedImage.TYPE_BYTE_INDEXED);
        final Graphics2D g2d = resizeImage.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
        RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING,
        RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_DITHERING,
        RenderingHints.VALUE_DITHER_ENABLE);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
        RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
        RenderingHints.VALUE_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
        RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
        RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
        RenderingHints.VALUE_STROKE_NORMALIZE);
        g2d.drawImage(image, 0, 0, width, height, null);
        return resizeImage;
    }
}
